package day240519_report.e4;

/*
 * 需求：
 * 求若干个圆形，三角形，长方形的周长和面积的总和。
 * */

public class Test {
    public static void main(String[] args) {
        Shape[] shapes = new Shape[6];

        Circle circle1 = new Circle();
        Triangle triangle1 = new Triangle();
        Rectangle rectangle1 = new Rectangle();

        Circle circle2 = new Circle();
        Triangle triangle2 = new Triangle();
        Rectangle rectangle2 = new Rectangle();

        circle1.setRadius(2.5);
        triangle1.setEdge1(3).setEdge2(4).setEdge3(5);
        rectangle1.setLength(8).setWidth(4);

        circle2.setRadius(5);
        triangle2.setEdge1(7).setEdge2(3).setEdge3(5);
        rectangle2.setLength(6).setWidth(5);

        // 父类引用指向子类对象
        shapes[0] = circle1;
        shapes[1] = circle2;
        shapes[2] = triangle1;
        shapes[3] = triangle2;
        shapes[4] = rectangle1;
        shapes[5] = rectangle2;

        double sum = 0;
        for (int i = 0; i < shapes.length; i++) {
            sum += shapes[i].getPerimeter();
            sum += shapes[i].getArea();
        }

        System.out.println("sum = " + sum);
    }
}

/*
 * 多态：
 * 同一个方法在不同的对象上具有不同的行为
 * 允许不同的对象对同一个消息做出不同的响应
 * 提高代码的灵活性和可扩展性
 * */
